package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import dto.ChestionareDTO;
import dto.EmployeeDTO;
import entities.Chestionare;
import entities.RaspunsuriCompletate;

@Stateless
public class ChestionareDAO implements ChestionareDAORemote{
	
	@PersistenceContext
	EntityManager em;
	
	Conversion conv = new Conversion();

	@Override
	public List<ChestionareDTO> findAllChestionare() {
		Query query = em.createNamedQuery("getToateChestionarele");
		List<Chestionare> chestionare = query.getResultList();
		List<ChestionareDTO> result = new ArrayList<ChestionareDTO>();
		for (int i = 0; i < chestionare.size(); i++) {
			ChestionareDTO c = new ChestionareDTO();
			c = conv.convertChestionareToDTO(chestionare.get(i));
			result.add(c);
		}
		return result;
	}

	@Override
	public void create(ChestionareDTO chestionareDTO) {
		try {
			Chestionare c = new Chestionare();
			c.setNume(chestionareDTO.getNume());
			c.setScop(chestionareDTO.getScop());
			c.setCategorie(chestionareDTO.getCategorie());
			c.setSubcategorie(chestionareDTO.getSubcategorie());
			c.setActiv(chestionareDTO.getActiv());

			em.persist(c);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void delete(ChestionareDTO chestionare) {
		int id = chestionare.getId();
		Chestionare c = new Chestionare();
		c = em.find(Chestionare.class, id);
		em.remove(c);
		
	}

	@Override
	public void update(ChestionareDTO c) {
		int id = c.getId();
		Chestionare chest= new Chestionare();
		chest = em.find(Chestionare.class, id);
		chest.setNume(c.getNume());
		chest.setScop(c.getScop());
		chest.setCategorie(c.getCategorie());
		chest.setSubcategorie(c.getSubcategorie());
		chest.setActiv(c.getActiv());
		
	}

	@Override
	public List<ChestionareDTO> getChestionareDupa(String dupa, String valoare) {
		if ("Categorie".equals(dupa)) {
			Query query = em.createNamedQuery("dupaCategorie");
			query.setParameter("cat_Param", valoare);
			List<Chestionare> chestionare = query.getResultList();
			List<ChestionareDTO> che = new ArrayList<ChestionareDTO>();// = new

			for (int i = 0; i < chestionare.size(); i++) {
				ChestionareDTO c = new ChestionareDTO();
				c.setId(chestionare.get(i).getId());
				c.setNume(chestionare.get(i).getNume());
				c.setScop(chestionare.get(i).getScop());
				c.setCategorie(chestionare.get(i).getCategorie());
				c.setSubcategorie(chestionare.get(i).getSubcategorie());
				c.setActiv(chestionare.get(i).getActiv());
				if (c.getActiv() == 1)
					che.add(c);
			}

			return che;
		}
		else if ("Subcategorie".equals(dupa)){
			Query query = em.createNamedQuery("dupaSubCategorie");
			query.setParameter("subcat_Param", valoare);
			List<Chestionare> chestionare = query.getResultList();
			List<ChestionareDTO> che = new ArrayList<ChestionareDTO>();// = new

			for (int i = 0; i < chestionare.size(); i++) {
				ChestionareDTO c = new ChestionareDTO();
				c.setId(chestionare.get(i).getId());
				c.setNume(chestionare.get(i).getNume());
				c.setScop(chestionare.get(i).getScop());
				c.setCategorie(chestionare.get(i).getCategorie());
				c.setSubcategorie(chestionare.get(i).getSubcategorie());
				c.setActiv(chestionare.get(i).getActiv());
				if (c.getActiv() == 1)
					che.add(c);
			}

			return che;
		}
		else if ("Scop".equals(dupa)){
			Query query = em.createNamedQuery("dupaScop");
			query.setParameter("scop_Param", valoare);
			List<Chestionare> chestionare = query.getResultList();
			List<ChestionareDTO> che = new ArrayList<ChestionareDTO>();// = new

			for (int i = 0; i < chestionare.size(); i++) {
				ChestionareDTO c = new ChestionareDTO();
				c.setId(chestionare.get(i).getId());
				c.setNume(chestionare.get(i).getNume());
				c.setScop(chestionare.get(i).getScop());
				c.setCategorie(chestionare.get(i).getCategorie());
				c.setSubcategorie(chestionare.get(i).getSubcategorie());
				c.setActiv(chestionare.get(i).getActiv());
				if (c.getActiv() == 1)
					che.add(c);
			}

			return che;
		}
		return null;
	}

	@Override
	public List<ChestionareDTO> findAllChestionareActiv() {
		Query query = em.createNamedQuery("dupaActiv");
		query.setParameter("activ_Param", 1);
		List<Chestionare> chestionare = query.getResultList();
		List<ChestionareDTO> result = new ArrayList<ChestionareDTO>();
		EmployeeDTO e = new EmployeeDTO();
		FacesContext fc = FacesContext.getCurrentInstance();
		e = (EmployeeDTO)fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		Query q = em.createNamedQuery("findAllRaspunsuriCompletate");
		List<RaspunsuriCompletate> rc = q.getResultList();
		for (int i = 0; i < chestionare.size(); i++) {
			ChestionareDTO c = new ChestionareDTO();
			c = conv.convertChestionareToDTO(chestionare.get(i));
			boolean b = true;
			for (int j = 0; j < rc.size(); j++){
				if (e.getId() == rc.get(j).getAngajatId())
					if (c.getId() == rc.get(j).getChestionarId())
						b = false;
			}
			if (b == true)
				result.add(c);
		}
		return result;
	}
	
	

}
